/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-service-JDInterface
*
* @File name : JDInterfaceController.java
*
* @Author : Administrator
*
* @Date : 2017年4月11日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年4月11日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/

package com.yonyou.gmmc.service.JDInterface.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.log4j.Logger;
import org.marketing.common.constants.ResultMsgConstants;
import org.marketing.common.exception.JDIntefaceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import org.marketing.common.Response;
import com.yonyou.gmmc.service.JDInterface.constant.JDInterfaceConstant;
import com.yonyou.gmmc.service.JDInterface.service.JDInterfaceService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author Administrator TODO description
 * @date 2017年4月11日
 */
@RestController
@RequestMapping(value = JDInterfaceConstant.BASE_PATH + "/" + JDInterfaceConstant.VERSION)
@Api("京东接口")
public class JDInterfaceController {

    private Logger             logger = Logger.getLogger(JDInterfaceController.class);

    @Autowired
    private JDInterfaceService jdInterfaceService;

    /**
     * 获取京东TOKEN
     * 
     * @author Qiankun
     * @date 2017年4月12日
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/token", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "获取京东TOKEN", notes = "获取京东TOKEN")
    public String getToken() {
        String token = "";
        try {
            token = jdInterfaceService.getToken();
            logger.info("toekn为：" + token);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new JDIntefaceException("获取token异常");
        }
        return token;
    }

    /**
     * 更新TOKEN
     * 
     * @author Administrator TODO description
     * @date 2017年4月12日
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/refreshToken", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "更新TOKEN", notes = "更新TOKEN")
    public String refreshToken(){
        String token = "";
        try {
            token = jdInterfaceService.refreshToken();
            logger.info("toekn为：" + token);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new JDIntefaceException("刷新token异常");
        }
        return token;
    }

    /**
     * 7.1 统一下单接口
     * 
     * @author Administrator TODO description
     * @date 2017年4月14日
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/JDOrder", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public @ResponseBody Map<String, Object> submitOrder(@RequestBody Map<String, Object> queryMap) throws Exception {
//        String thirdOrder = queryMap.get("thirdOrder").toString();
//        String userId = queryMap.get("userId").toString();
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try{
                resultMap = jdInterfaceService.submitOrder(queryMap);
        }catch (Exception e){
            logger.error(e.getMessage(), e);
            throw new JDIntefaceException("下单异常");
        }
        logger.info("返回结果——" + resultMap);
        return resultMap;
    }

    /**
     * 保存一级地址
     * 
     * @author Administrator TODO description
     * @date 2017年4月14日
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/insertAddress", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "保存一级地址", notes = "保存一级地址")
    public @ResponseBody Map<String, Object> insertAddress() {
        try {
            // jdInterfaceService.truncateAddress();
            String token = jdInterfaceService.getToken();
            jdInterfaceService.insertAddress(token);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_SUCCESS);
            return resultMap;
        } catch (Exception e) {
            logger.error("保存一级地址发生异常", e);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
            return resultMap;
        }

    }

    /**
     * 保存二级地址
     * 
     * @author Administrator TODO description
     * @date 2017年4月14日
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/insertSecondAdd", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "保存二级地址", notes = "保存二级地址")
    public @ResponseBody Map<String, Object> insertSecondAdd() {
        try {
            String token = jdInterfaceService.getToken();
            jdInterfaceService.insertSecondAdd(token);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_SUCCESS);
            return resultMap;
        } catch (Exception e) {
            logger.error("保存二级地址发生异常", e);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
            return resultMap;
        }
    }

    /**
     * 保存三级地址
     * 
     * @author Administrator TODO description
     * @date 2017年4月14日
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/insertThirdAdd", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "保存三级地址", notes = "保存三级地址")
    public @ResponseBody Map<String, Object> insertThirdAdd() {
        try {
            String token = jdInterfaceService.getToken();
            jdInterfaceService.insertThirdAdd(token);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_SUCCESS);
            return resultMap;
        } catch (Exception e) {
            logger.error("保存三级地址发生异常", e);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
            return resultMap;
        }
    }

    /**
     * 保存四级地址
     * 
     * @author Administrator TODO description
     * @date 2017年4月14日
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/insertFourthAdd", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "保存四级地址", notes = "保存四级地址")
    public @ResponseBody Map<String, Object> insertFourthAdd() {
        try {
            String token = jdInterfaceService.getToken();
            logger.info("==>token值：" + token);
            jdInterfaceService.insertFourthAdd(token);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_SUCCESS);
            return resultMap;
        } catch (Exception e) {
            logger.error("保存四级地址发生异常", e);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
            return resultMap;
        }
    }

    /**
     * 保存JD所有地址
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/insertAllAdd", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "保存所有地址", notes = "保存所有地址")
    public @ResponseBody Map<String, Object> insertAllAdd() {
        try {
            // 清空地址表数据
            // jdInterfaceService.truncateAddress();
            // 获取token
            String token = jdInterfaceService.getToken();
            logger.info("==>token值：" + token);
            // 插入一级地址
            jdInterfaceService.insertAddress(token);
            // 插入二级地址
            jdInterfaceService.insertSecondAdd(token);
            // 插入三级地址
            jdInterfaceService.insertThirdAdd(token);
            // 插入四级地址
            jdInterfaceService.insertFourthAdd(token);

            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_SUCCESS);
            return resultMap;
        } catch (Exception e) {
            logger.error("保存所有地址发生异常", e);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
            return resultMap;
        }
    }

    /**
     * 校验JD地址
     * 
     * @param provinceId
     * @param cityId
     * @param countyId
     * @param townId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/checkFourAdd", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "校验JD地址", notes = "校验JD地址")
    public @ResponseBody Response<List<Map<String, Object>>> checkFourAdd(@RequestBody Map<String, Long> params){
        try {
            // 获取token
            String token = jdInterfaceService.getToken();
            logger.info("==>token值：" + token);
            // 获取参数
            Long provinceId = params.get("provinceId");
            Long cityId = params.get("cityId");
            Long countyId = params.get("countyId");
            Long townId = params.get("townId");
            // 校验地址
            List<Map<String, Object>> result = jdInterfaceService.checkFourAdd(token, provinceId, cityId, countyId,
                                                                               townId);
            logger.info("==>result值.........：" + result);
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("校验JD地址发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }

    /**
     * 取消未确认订单接口
     * 
     * @param jdOrderId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/cancelJDOrder", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "取消未确认订单接口", notes = "取消未确认订单接口")
    public @ResponseBody Response<List<Map<String, Object>>> cancelOrder(@RequestParam String jdOrderId) {
        try {
            List<Map<String, Object>> result = jdInterfaceService.cancelOrder(jdOrderId);
            logger.info("==>result值.........：" + result);
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("取消未确认订单发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }

    }

    /**
     * 商品区域购买限制查询
     * 
     * @author lyq
     * @date 2017年4月17日
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/checkAreaLimit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "商品区域购买限制查询", notes = "商品区域购买限制查询")
    public @ResponseBody Response<List<Map<String, Object>>> checkAreaLimit(@RequestBody Map<String, Object> params) {
        try {

            List<Map<String, Object>> list = jdInterfaceService.checkAreaLimit(params);
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, list, "");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }

    /**
     * 批量获取库存接口（建议订单详情页、下单使用）
     * 
     * @author lyq
     * @date 2017年4月17日
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getNewStockById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "批量获取库存接口（建议订单详情页、下单使用）", notes = "批量获取库存接口（建议订单详情页、下单使用）")
    public @ResponseBody Response<List<Map<String, Object>>> getNewStockById(@RequestBody Map<String, Object> params) {
        try {

            List<Map<String, Object>> list = jdInterfaceService.getNewStockById(params);
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, list, "");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }

    /**
     * 批量获取库存接口(建议商品列表页使用)
     * 
     * @author lyq
     * @date 2017年4月17日
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getStockById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "批量获取库存接口(建议商品列表页使用)", notes = "批量获取库存接口(建议商品列表页使用)")
    public @ResponseBody Response<List<Map<String, Object>>> getStockById(@RequestBody Map<String, Object> params) {
        try {
            List<Map<String, Object>> list = jdInterfaceService.getStockById(params);
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, list, "");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }

    /**
     * 支付接口
     * 
     * @author Administrator TODO description
     * @date 2017年4月27日
     * @param jdOrderId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/order/doPay", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public @ResponseBody Map<String, Object> doPay(@RequestParam("jdOrderId") String jdOrderId) throws Exception {
        Map<String, Object> resultMap = jdInterfaceService.doPay(jdOrderId);
        logger.info("返回结果——" + resultMap);
        return resultMap;
    }

    /**
     * 确认预占库存订单接口
     * 
     * @author Administrator TODO description
     * @date 2017年5月3日
     * @param params
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/confirmOrder", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "确认预占库存订单接口", notes = "确认预占库存订单接口")
    public @ResponseBody Response<List<Map<String, Object>>> confirmOrder(@RequestBody Map<String, Object> params) {
        try {
            List<Map<String, Object>> list = jdInterfaceService.confirmOrder(params);
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, list, "");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }

    /**
     * 查询京东订单信息接口
     * 
     * @author Administrator TODO description
     * @date 2017年5月9日
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/JdOrders", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "查询京东订单信息接口", notes = "查询京东订单信息接口")
    public @ResponseBody Map<String, Object> selectJdOrder() {
        Map<String, Object> result = new HashMap<>();
        try {
            result = jdInterfaceService.selectJdOrder();
            logger.info("返回信息");
        } catch (Exception e) {
            logger.error("调用接口发生异常", e);
        }
        return result;
    }

    /**
     * 7.9统一余额查询接口
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getBalance", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "统一余额查询接口", notes = "统一余额查询接口")
    public @ResponseBody Response<List<Map<String, Object>>> getBalance(@RequestBody Map<String, Integer> param) {
        try {
            String token = jdInterfaceService.getToken();
            List<Map<String, Object>> result = jdInterfaceService.getBalance(token, param);
            logger.info("==>result值.........：" + result);
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("统一余额查询发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }

    /**
     * 7.11余额明细查询接口
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getBalanceDetail", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "余额明细查询接口", notes = "余额明细查询接口")
    public @ResponseBody Map<String, Object> getBalanceDetail() {
        try {
            String token = jdInterfaceService.getToken();
            logger.info("==>token值：" + token);
            jdInterfaceService.pageBalanceDetail(token);
            Map<String, Object> resultMap = new HashMap<>();
        	resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_SUCCESS);
            return resultMap;
        } catch (Exception e) {
            logger.error("余额明细查询发生异常", e);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
            return resultMap;
        }
    }
    
    /**
     * 7.10查询用户金彩余额接口
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/selectJincaiCredit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "查询用户金彩余额接口", notes = "查询用户金彩余额接口")
    public @ResponseBody Response<Map<String, Object>> selectJincaiCredit() {
        try {
            String token = jdInterfaceService.getToken();
            Map<String, Object> resultMap=jdInterfaceService.selectJincaiCredit(token);
            logger.info("==>resultMap值.........：" + resultMap);
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, resultMap, "");
        } catch (Exception e) {
            logger.error("查询用户金彩余额发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }
}
